use std::{collections::HashMap, vec};

use container_core::workflow::{SimpleWorkflowEngine, WorkflowDocumentJsonReader};
use model_graph_types::{
    container::workflow::{
        WorkflowDocumentReadeOptions, WorkflowDocumentReader, WorkflowEngine,
        WorkflowEngineCompileOptions, WorkflowEngineRunOptions, WorkflowGraphNode,
        WorkflowGraphNodeData, WorkflowGraphNodeHttpStatement, WorkflowGraphNodeStatement,
    },
    database::{ConnectorProtocol, DataSource as DatabaseDataSource, DatabaseType},
    datasource::{
        ClassRelationCondition, ClassRelationConditionBuilder, DataSourceConnector,
        MySQLStorageOption, QueryCondition, QueryConditionBuilder, QueryExtention,
        QueryExtentionBuilder, QueryOptions as DataSourceConnectorQueryOptions, RelationCondition,
        RelationConditionBuilder, SQLiteStorageOption, Storage,
    },
    modeling::{
        assistant::{FormSchema, FormSchemaItem, ModelProvider},
        meta::SYSTEM_DOMAIN_IDENTIFIER,
        set_system_meta_database, system_meta_database, Class, ClassGetter, ClassRelation,
        DataSourceEntity, Domain, MetaConnector, RelationDirection, Value,
    },
};

use datasource_connector::{
    connector::SimpleDataSourceConnector, modeling_connector::SimpleMetaConnector,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    set_system_meta_database(DatabaseDataSource {
        id: 0,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/rrg01.sqlite"),
        ..Default::default()
    })?;

    let mut res = SimpleMetaConnector::new_with_meta_datasource(&Some(system_meta_database()?))?
        .get_class(&String::from("b8a44d4d-9fe6-4f86-95a6-e0881f3b55a5"))
        .await;

    match res {
        Ok(response) => {
            tracing::debug!("{}", serde_json::to_string(&response).expect(""));
        }
        Err(err) => {
            tracing::error!("{:?}", err);
        }
    }

    // let mut conditions = QueryCondition::with_relations(vec![
    //     ClassRelationCondition::with_class_codition(
    //         RelationCondition::with_class_identifier(
    //             RelationDirection::Reverse,
    //             String::from("rrg01_desktop_project"),
    //         )?,
    //         QueryCondition::with_attribute(
    //             String::from("project_identifier"),
    //             String::from("rrg01_proj_0001"),
    //         )?,
    //     )?,
    //     ClassRelationCondition::with_class_codition(
    //         RelationCondition::with_class_identifier(
    //             RelationDirection::Reverse,
    //             String::from("rrg01_project_application"),
    //         )?,
    //         QueryCondition::with_attribute(
    //             String::from("application_identifier"),
    //             String::from("test_app_002"),
    //         )?,
    //     )?,
    // ])?;

    // conditions.set_page(1, 10)?;

    // let mut res = SimpleDataSourceConnector::default()
    //     .instance_query(
    //         &None,
    //         &datasource2,
    //         &Domain::get_identifier()?,
    //         conditions,
    //         None,
    //         None,
    //     )
    //     .await;

    // match res {
    //     Ok(response) => {
    //         tracing::debug!("{:?}", response);
    //     }
    //     Err(err) => {
    //         tracing::error!("{:?}", err);
    //     }
    // }
    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
